package com.zhugang.week13.hash;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @program algorithms
 * @description: ConcurrentMultiValueMap
 * @author: chanzhugang
 * @create: 2022/12/07 12:20
 */
public class ConcurrentMultiValueMap<K, V> {

    private final Map<K, List<V>> targetMap = new ConcurrentHashMap<>();

    public List<V> get(K key) {
        return this.targetMap.get(key);
    }

    public boolean put(K key, V value) {
        List<V> values = this.targetMap.computeIfAbsent(key, k -> new LinkedList<>());
        return values.add(value);
    }

    public boolean remove(K key, V value) {
        List<V> values = this.targetMap.get(key);
        if (values != null && values.size() > 0) {
            return values.remove(value);
        }
        return false;
    }

    public static void main(String[] args) {
        ConcurrentMultiValueMap<String, String> valueMap = new ConcurrentMultiValueMap<>();
        valueMap.put("1", "1");
        valueMap.put("1", "11");
        valueMap.put("1", "111");
        valueMap.put("1", "1111");
        List<String> list1 = valueMap.get("1");
        System.out.println(list1);
        boolean remove = valueMap.remove("1", "1111");
        System.out.println(remove);

        List<String> list2 = valueMap.get("1");
        System.out.println(list2);
        /*Map<String, List<String>> map = new ConcurrentHashMap<>();
        map.put("1", Arrays.asList("11", "22"));
        map.put("1", Arrays.asList("33", "44"));
        System.out.println(map);*/


    }


}